package com.ruoyi.web.controller.business.rule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.rule.domain.JbRules;
import com.ruoyi.business.rule.service.JbRulesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.business.rule.factory.JbRulesFactory;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 业务规则 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("业务规则")
@RestController
@RequestMapping("/rules")
public class JbRulesController extends JoController
{
    @Autowired
    private JbRulesService service;
    @Autowired
    private JbRulesFactory factory;

    /**
     * 查询业务规则列表
     */
    @ApiOperation("查询业务规则列表")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbRules jbRules)
    {
        LambdaQueryWrapper<JbRules> select = new LambdaQueryWrapper<>();
        select.eq(JbRules::getRulesCode, jbRules.getRulesCode());
        IPage<JbRules> iPage = service.page(page.b(), select);
        return JoR.rows(iPage);
    }

    /**
     * 导出业务规则列表
     */
    @ApiOperation("导出业务规则列表")
    @Log(title = "业务规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbRules jbRules)
    {
        LambdaQueryWrapper<JbRules> select = new LambdaQueryWrapper<>();
        IPage<JbRules> iPage = service.page(page.b(), select);

        List<JbRules> list = iPage.getRecords();
        ExcelUtil<JbRules> util = new ExcelUtil<JbRules>(JbRules.class);
        util.exportExcel(response, list, "业务规则数据");
    }

    /**
     * 获取业务规则详细信息
     */
    @ApiOperation("获取业务规则详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 获取业务规则详细信息 根据code编码
     */
    @ApiOperation("获取业务规则详细信息")
    @GetMapping(value = "/rulesCode/{rulesCode}")
    public AjaxResult getInfoByRulesCode(@PathVariable("rulesCode") String rulesCode)
    {
        LambdaQueryWrapper<JbRules> select = new LambdaQueryWrapper<>();
        select.eq(JbRules::getRulesCode, rulesCode);
        return success(service.getOne(select));
    }

    /**
     * 新增业务规则
     */
    @ApiOperation("新增业务规则")
    @PreAuthorize("@ss.hasPermi('rules:change')")
    @Log(title = "业务规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbRules jbRules)
    {
        return toAjax(service.save(jbRules));
    }

    /**
     * 修改业务规则
     */
    @ApiOperation("修改业务规则")
    @PreAuthorize("@ss.hasPermi('rules:change')")
    @Log(title = "业务规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbRules jbRules)
    {
        return toAjax(factory.edit(jbRules));
    }

    /**
     * 删除业务规则
     */
    @ApiOperation("删除业务规则")
    @PreAuthorize("@ss.hasPermi('rules:change')")
    @Log(title = "业务规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
}
